const app = getApp()

import {  effectiveCustomer } from "../../common/static/api_data.js"
import { throttle } from '../../utils/throttle.js'
import { mobileModel } from '../../utils/method.js'
import { HiddenCall } from '../../request/api.js'
const request = new HiddenCall
Component({
  /**
*  添加跟进描述
* - @author           王磊
* - @date             2018-07-10
* mobileModel     获取机型
* effective             跟进类型
* isIos                   是否是ios系统
* isDetailFollow    是否是详情跟进还是电话跟进
* callId                  电话id
* followshow        打开详情跟进开关
* isType                跟进还是描述索引      
* utype                 房源或客源
* description        描述
* followUpType    跟进
* iseffective          有效或无效跟进
* isIos                   是否为苹果
*/
  properties: {
    utype: String, 
    isType: Number,
    code: String,
    isDetailFollow: String,
    callId: String,
    followshow: {
      type: Boolean,
      value: false
    },
    mainType: String,
    resourceType: String
  },
  data: {
    effective: [],
    effectiveCustomer,
    followUpType: [],
    invalidFollowUpType: [],
    dataText: {
      description: "",
      followup: "",
      iseffective: null,
      followTypeLower: null
    },
    followList: [
      { headerTitle: "跟进" }, { headerTitle: "描述" }
    ],
    isIos: false,
    isTestFollow: true,
    isRequest: false //点击确定按钮后置为true，button不能再次点击
  },
  /**
   * 获取用户机型添加不同的样式
   * author wl
   */
  ready() {
    mobileModel().then((res) => {
      // true 为ios
      this.setData({ isIos: res})
    })
    this.setData({
      effective: app.globalData.followUpType
    })
  },
  methods: {
    /**
     * 选择跟进有效跟进||无效跟进
     * author wl
     */
    selectRadio(e) {
      // 获取选择跟进类型索引
      let oindex = e.currentTarget.dataset.oindex[0]
      // 获取选择跟进类型的值
      let value = e.currentTarget.dataset.oindex[1]
      // 赋予数据model
      this.data.dataText.iseffective = value
      // 渲染选择的类型
      let model = this.data.followUpType.map((el) => {
        el.checked = false
        return el
      })
      // 跟进备注为空
      this.data.dataText.followup = ""
      this.data.invalidFollowUpType.forEach((el) => { el.checked = false })
      this.data.dataText.followTypeLower = null
      // 数据更新
      this.setData({
        radioIndex: oindex,
        dataText: this.data.dataText,
        followUpType: model,
        invalidFollowUpType: this.data.invalidFollowUpType
      })
      this.effectiveFollowUpType(value, oindex)
      this.testFollowup()
    },
    /**
     * 获取有效下级
     */
    effectiveFollowUpType(value, oindex) {
      request.subordinateType(value, this.data.utype).then((res) => {
        oindex === 0 ? this.setData({ followUpType: res }) : this.setData({ invalidFollowUpType: res})
      })
    },
    /**
     * 提交跟进||描述
     * author wl
     */
    formSubmit: throttle(function (e) {
      // 获取提交的数据
      let data = this.data.dataText
      // 如果跟进备注有值赋值
      if (e.detail.value.followup) {
        data.followup = e.detail.value.followup
        this.setData({ btnLoading: true })
      }
      // 如果描述有值赋值
      if (e.detail.value.description) {
        data.description = e.detail.value.description
        this.setData({ btnLoading: true })
      }
      // 数据汇集
      let followup = {
        // 手机号id
        callId: this.data.callId,
        // 根据索引将跟进或描述赋值
        content: this.data.isType === 0 ? data.followup : data.description,
        // 跟进类型有效或无效
        followType: data.iseffective,
        // 房客标号
        resourceCode: this.data.code,
        // 房源客源
        resourceType: null,
        followTypeLower: data.followTypeLower,
        utype: this.data.utype
      }
      if (!data.followTypeLower && this.data.isType === 0) {
        wx.showToast({ title: '请选择完整跟进！', icon: 'none'})
        this.setData({ btnLoading: false })
        return
      }
      // 是否为房客源详情
      if (this.data.isDetailFollow === 'detailFollow') {
        // 删除属性电话id
        delete followup.callId
        // 调用添加跟进
        this.addFollowUp(this.data.isType, followup, data, e)
        // 调用添加描述
        this.addDescription(this.data.isType, followup, data, e)
        // 将数据设置为无
        data = undefined
      } else {
        this.setData({ btnLoading: false })
        // 打电话之后添加跟进描述，调用父formSubmit，传入相关数据
        if (data.description && data.description.length > 0 && data.description.length < 5) {
          wx.showToast({ title: '请选择完整描述！', icon: 'none' })
          return
        }
        this.triggerEvent('formSubmit', { formId: e.detail.formId, type: undefined, data, followup })
      }
    }, 2000),
    /**
     * 确认添加提交描述
     * author wl
     */
    addDescription(isType, followup, data, e) {
      // 描述
      if (isType === 1) {
        //确定按钮不可再次点击
        this.setData({
          isRequest: true
        })
        // 友好提示
        wx.showLoading({ title: '添加中...' })
        // 如果描述符合调用添加描述接口
        if (data.description && data.description.length >= 5) {
          request.addDescription(followup).then((res) => {
            //确定按钮可点击
            this.setData({
              isRequest: false
            })
            // 关闭提示
            wx.hideLoading()
            // 关闭添加窗口清空数据
            this.cancel()
            // 描述添加成功后提示
            this.triggerEvent('formSubmit', { formId: e.detail.formId, type: undefined, prompt: true })
            this.triggerEvent('addDescription', { tactics: "queryDescriptionInfoServiceImpl", code: followup.resourceCode })
            this.setData({ btnLoading: false})
            // 友好提示
          })
        } else {
          wx.showToast({ title: '请填写完整描述！', icon: 'none' })
          this.setData({ btnLoading: false })
          //如果提交的文本不符合要求，设置确定按钮可再次点击
          this.setData({
            isRequest: false
          })
        }
      }
    },
    /**
     * 确认添加提交跟进
     * author wl
     */
    addFollowUp: function (isType, followup, data, e) {
      if (data.followup.indexOf('"') !== -1 || data.followup.indexOf("'") !== -1 || data.followup.indexOf("&") !== -1 || data.followup.indexOf("\\") !== -1 || data.followup.indexOf("_") !== -1) {
        wx.showToast({ title: '请不要输入特殊符号！', icon: 'none' })
        return
      }
      // 跟进
      if (isType === 0) {
        //确定按钮不可再次点击
        this.setData({
          isRequest: true
        })
        // 友好提示
        wx.showLoading({ title: '添加中...' })
        // 如果跟进符合调用添加跟进接口
        if (data.followup && data.followup.length >= 2 && data.iseffective !== "") {
          request.addFollowup(followup).then((res) => {
            //确定按钮可点击
            this.setData({
              isRequest: false
            })
            // 关闭提示
            wx.hideLoading()
            // 关闭添加窗口清空数据
            this.cancel()
            // 跟进添加成功后提示
            this.triggerEvent('formSubmit', { formId: e.detail.formId, type: undefined, prompt: true })
            this.triggerEvent('addFollow', { tactics: "queryFollowUpInfoServiceImpl", code: followup.resourceCode })
            this.setData({ btnLoading: false })
          })
        } else {
          // 友好提示
          wx.showToast({ title: '请填写完整跟进！', icon: 'none' })
          this.setData({ btnLoading: false })
          //如果提交的文本不符合要求，设置确定按钮可再次点击
          this.setData({
            isRequest: false
          })
        }
      }
    },
    /**
     * 取消添加跟进||描述
     * author wl
     */
    cancel() {
      // 清空描述
      this.data.dataText.description = ""
      // 清空跟进
      this.data.dataText.followup = ""
      // 延时更新数据
      setTimeout(() => {
        this.setData({
          dataText: this.data.dataText,
          radioIndex: 3
        })
      }, 300)
      // 调用关闭新增描述跟进窗口
      this.triggerEvent('clickMask')
    },
    /**
   * 获取要添加描述文本
   * author wl
   */
    valueTextarea(e) {
      // 获取用户输入的描述文本
      this.data.dataText.description = e.detail.value
      // 更新添加描述跟进数据
      this.setData({
        dataText: this.data.dataText,
        valueText: e.detail.value
      })
      this.testFollowup()
    },
    /**
    * 获取要添加跟进文本
    * author wl
    */
    valueFollowarea(e) {
      // 获取用户输入的跟进文本
      this.data.dataText.followup = e.detail.value
      // 更新添加描述跟进数据
      this.setData({ dataText: this.data.dataText })
    },
    /**
   * 切换跟进||描述选项
   * author wl
   */
    followUpDescription(e) {
      // 获取当前选择的索引
      let index = e.currentTarget.dataset.index
      // 索引赋予描述跟进索引
      this.setData({
        isType: index,
        dataText: this.data.dataText
      })
    },
    /**
     * 选择跟进有效跟进
     * author wl
     */
    followChange (e) {
      // 调用子组件事件
      this.data.dataText.iseffective = 1
      e.detail.model.forEach((el) => {
        if (el.checked) {
          this.data.dataText.followTypeLower = el.code
        }
      })
      this.data.invalidFollowUpType.forEach((el) => { el.checked = false})
      //更新数据
      this.setData({
        followUpType: e.detail.model,
        dataText: this.data.dataText,
        radioIndex: 0,
        invalidFollowUpType: this.data.invalidFollowUpType
      })
      this.testFollowup()
    },
    /**
     * 选择跟进无效跟进
     * author wl
     */
    invalidFollowChange(e) {
      // 调用子组件事件
      // 获取选中无效跟进的值
      this.data.dataText.iseffective = 2
      e.detail.model.forEach((el) => {
        if (el.checked) {
          this.data.dataText.followTypeLower = el.code
        }
      })
      this.data.followUpType.forEach((el) => { el.checked = false })
      // // 更新数据
      this.setData({
        invalidFollowUpType: e.detail.model,
        dataText: this.data.dataText,
        radioIndex: 1,
        followUpType: this.data.followUpType
      })
      this.testFollowup()
    },
    /**
     * 获取添加跟进备注值
     * author wl
     */
    remarkFollow(e) {
      // 将获取备注的值赋值数据model
      this.data.dataText.followup = e.detail.value
      // 更新数据model
      this.setData({
        dataText: this.data.dataText
      })
      this.testFollowup()
    },
     /**
     * 刷新跟进
     * author wl
     */
    bindRefresh () {
      this.triggerEvent('refreshType')
    },
    testFollowup () {
      let dataText = this.data.dataText
      if (dataText.followup.length>=2 && dataText.iseffective && dataText.followTypeLower) {
        this.setData({ isTestFollow: false})
      } else {
        this.setData({ isTestFollow: true })
      }
    }
  }
})